document.getElementById("charge").onclick=function(){
	var data={}
	data.account=$("#account").val()
	data.value=$("#value").val()
	data.addition=$("#addition").val()
	if(data.account.length!==11){
		showErrorInfo("会员账户格式不正确，应该是11位的手机号码！")
		return false;
	}
	var recommender=$("#recommend_chinese_name").attr("username")
	if (recommender===undefined || recommender===""){
		info2=""
	}
	else{
		data.recommender=recommender
		info2="\n推荐人账户: "+data.recommender + "    \n推荐人车牌号: "+$("#recommend_chinese_name").val()
	}

	if(/^ *$/.test(data.account)){
		showErrorInfo("会员账户不能为空！")
		return false;
	}
	if(/^ *$/.test(data.value)){
		showErrorInfo("充值金额不能为空！")
		return false;
	}
	t=confirm("请确认您的充值信息:\n会员账号: "+data.account+"\n会员姓名: "+$("#chinese_name").val() +"\n充值金额: ￥"+data.value+"\n赠送金额: ￥"+data.addition+"\n实际到账金额: ￥"+$("#realValue").val() +info2)
	if(!t){
		return false;
	}
	data.charge_type=$("input:radio[name='xx']:checked").attr("v")
	data=JSON.stringify(data)
	$.ajax({
		"url":"/charge/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"data":data},
		"type":"POST",
		"success":function(data){
			if(responseCheck(data)){
				alert("充值成功！")
				window.close()
			}
		}
	})
}
$("#title").text(getQ().title)
function parseData(){
	$.ajax({
		"url":"/get_charge_data/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
				if(data.type==="财务"|| data.type==="管理员"){
					$("#addition").attr("readonly",false)
				}
				else{
					$("#addition").attr("readonly",true)
				}
				/*充值优惠*/
				if(data.charge_level.length===0){
					showErrorInfo("当前没有充值优惠，请注意！")
				}
				$("#value").attr("exp",JSON.stringify(data.charge_level))
			}
		}
	})
}
document.getElementById("account").focus()
document.getElementById("account").onkeyup=function(){
	var account=$(this).val()
	if(account.length==11){
		$.ajax({
			"url":"/user/",
			"error":errorAjax,
			"beforeSend":start_load_pic,
			"complete":stop_load_pic,
			"data":{"user":account},
			"dataType":"jsonp",
			"success":function(data){
				if(responseCheck(data)){
					$("#chinese_name").val(data.number)
					$("#panel").show()
					$("#value").focus()
				}
			}
		})
	}
	if(account.length<11){
		$("#chinese_name").val()
		$("#value").val()
		$("#addition").val()
		$("#realValue").val()
		$("#panel").hide()
		
	}
	if(account.length>11){
		showErrorInfo("会员手机号不能大于11位数！")
		$("#chinese_name").val()
		$("#value").val()
		$("#addition").val()
		$("#realValue").val()
		$("#panel").hide()
	}
}
document.getElementById("tuijian_username").onkeyup=function(){
	var account=$(this).val()
	if(account.length==11){
		if(account===$("#account").val()){
			showErrorInfo("推荐人账户不能是被充值账户自己！")
			$("#recommend_chinese_name").attr("username","")
			$("#charge").hide()
			return false;
		}
		$.ajax({
			"url":"/user/",
			"error":errorAjax,
			"beforeSend":start_load_pic,
			"complete":stop_load_pic,
			"data":{"user":account},
			"dataType":"jsonp",
			"success":function(data){
				if(responseCheck(data)){
					$("#recommend_chinese_name").val(data.number)
					$("#recommend_chinese_name").attr("username",data.username)
					$("#showXName").show()
					$("#charge").show()
				}
			}
		})
	}
	if(account.length<11){
		$("#recommend_chinese_name").val()
		$("#recommend_chinese_name").attr("username","")
		$("#showXName").hide()
		if (account.length===0){
			$("#charge").show()
		}
		else{
			$("#charge").hide()
		}
		
	}
	if(account.length>11){
		showErrorInfo("会员手机号不能大于11位数！")
		$("#recommend_chinese_name").val()
		$("#showXName").hide()
		$("#recommend_chinese_name").attr("username","")
		$("#charge").hide()
	}
}
document.getElementById("addition").onkeyup=function(){
	var value=$("#value").val()
	var addition=$("#addition").val()
	$("#realValue").val(parseFloat(value)+parseFloat(addition))
}
document.getElementById("value").onkeyup=function(){
	var exp=this.getAttribute("exp")
	exp=JSON.parse(exp)
	var v=$(this).val()
	for(var i=0;i<exp.length;i++){
		current_index=i
		next_index=i+1
		if (exp[i][0]<=v){
			if(next_index===exp.length){
				//注意，i是0，不是1
				$("#addition").val(exp[i][1])
				$("#realValue").val(parseFloat(exp[i][1])+parseFloat(v))
				break
			}
			else if (v<exp[next_index][0]){
				$("#addition").val(exp[i][1])
				$("#realValue").val(parseFloat(exp[i][1])+parseFloat(v))
				break
			}
		}
		else{
			$("#addition").val(0)
			$("#realValue").val(v)
		}
	}
	if(exp.length===0){
		$("#addition").val(0)
		$("#realValue").val(v)
	}
}

parseData()
